import org.junit.Test;

import javax.swing.text.DateFormatter;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Locale;
import java.util.stream.Stream;

/**
 * @Authther: mrwu
 * @date:2021/3/23-03
 * @Description:PACKAGE_NAME
 */
public class DateDemo {

    @Test
    public void  datecreate(){
        //获得当前的时刻
        Instant now = Instant.now();
        //转换为时间戳
        long l = now.toEpochMilli();
        System.out.println(l);
        Instant now1 = Instant.now();
        System.out.println(now1.toEpochMilli());
        //计算连个时间的cha
        Duration between = Duration.between(now, now1);
        System.out.println(between);
        //将时间转换为某种计算单位的数值
        System.out.println(between.toNanos());

        //
        Instant instant = now.minusMillis(10l);//在当前时刻之前多久
        System.out.println(instant.toEpochMilli());
        Instant plus = now.plus(Duration.ofMillis(10));//之后多久
        System.out.println(plus.toEpochMilli());

        // duration 用来产生时间间隔的类



    }

    @Test
    public void localdate(){
        //本地日期
        LocalDate now2 = LocalDate.now();//获取当前日期
        System.out.println(now2);
        LocalDate of = LocalDate.of(2011, 12, 23);//产生一个本地日期
        System.out.println(of);

    }
    @Test
    public void  dateplus(){
        LocalDate localDate = LocalDate.now().plusDays(12);
        System.out.println(localDate);
        LocalDate of = LocalDate.of(2021, 11, 12);
        //将日期处理称流

        Stream<LocalDate> localDateStream = localDate.datesUntil(of);
        System.out.println(localDateStream.findFirst());

        //常用方法
        localDate.isLeapYear();//是否闰年
    }
    //日期调整器
    @Test
    public void testdateTemporalAdjusters(){

//        TemporalAdjusters 提供了很多方法 使用TemporalAdjusters 调整获取的日期
        //with 方法使用给定调整器调整后的结果
        LocalDate with = LocalDate.of(2021, 2, 12).with(TemporalAdjusters.lastDayOfMonth());
        System.out.println(with);
    }

    //本地时间
    @Test
    public void localtime(){
        LocalTime now = LocalTime.now();//获取当前时间
        System.out.println(now);
        //获取当前时间和日期
        LocalDateTime now1 = LocalDateTime.now();
        System.out.println(now1);
        //产生时间用of
        //修改时间用with

    }

    //时区时间
    @Test
    public void zoneTime(){
        ZonedDateTime now = ZonedDateTime.now();//获取当前时区的时间
        System.out.println(now);
    }
    //日期时间格式化
    @Test
    public void dateformat(){
        //Datetimeformatter
        String format = DateTimeFormatter.ISO_DATE_TIME.format(LocalDateTime.now());
        System.out.println(format);
        //可以使用of 方法创建格式器
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
        String format1 = dateTimeFormatter.format(LocalDateTime.now());
        System.out.println(format1);

        //withlocal 更改地区
        String format2 = dateTimeFormatter.withLocale(Locale.FRANCE).format(LocalDate.now());
        System.out.println(format2);
        //自定义日期格式
        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String format3 = dateTimeFormatter1.format(LocalDate.now());
        System.out.println(format3);

    }



}
